import { defineStore } from "pinia";
import { loginByPassword, getUserInfo, logout } from "src/api/user";

import { LocalStorage, SessionStorage } from "quasar";

import { ACCESS_TOKEN } from "src/stores/mutation-types";

export const useUserStore = defineStore("userStore", {
  state: () => ({
    token: "",
    user: {},
  }),
  getters: {},
  actions: {
    LoginByPassword(parameters) {
      console.log("store", parameters);
      return new Promise((resolve, reject) => {
        loginByPassword(parameters)
          .then((res) => {
            console.log("res", res.result);
            SessionStorage.set(
              ACCESS_TOKEN,
              res.result.token,
              7 * 24 * 60 * 60 * 1000
            );
            // commit("SET_TOKEN", res.token);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    Logout() {
      return new Promise((resolve, reject) => {
        logout()
          .then((res) => {
            SessionStorage.remove(ACCESS_TOKEN);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    GetUserInfo() {
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then((res) => {
            resolve();
          })
          .catch((error) => {
            reject(error);
          })
          .finally();
      });
    },
  },
});
